package com.net.tool;

import java.lang.reflect.Constructor;

import com.net.tool.BasicDownload.DownloadTable;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class CheckExcaptionTask extends DownLoadRunable {

	public CheckExcaptionTask(Context context) {
		super(context);
	}

	protected BasicDownload getNextExcuteTask() {

		synchronized (BasicDownload.class) {

			BasicDownload mNextbasicDownload = null;

			Cursor cursor = BasicDownload.getSqlite(mContext).query(
					DownloadTable.TB_NAME,
					new String[] { DownloadTable.TASKID, DownloadTable.TASKURL,
							DownloadTable.PRIORITY, DownloadTable.SERVER_VC,
							DownloadTable.TASKSTATE, DownloadTable.CLASS },
					DownloadTable.TASKSTATE + "=?",
					new String[] { BasicDownload.DOWNLOAD_STATTE_DOWNLOADING
							+ "" }, null, null,
					DownloadTable.PRIORITY + " desc");

			if (cursor.moveToFirst()) {

				try {
					Log.d("class", cursor.getString(5));
					Class<?> classType = Class.forName(cursor.getString(5));
					Constructor<?> constructor = classType
							.getDeclaredConstructor(new Class[] { Context.class });
					mNextbasicDownload = (BasicDownload) constructor
							.newInstance(mContext);
					Log.d("download", "反射实例化成功");
					mNextbasicDownload.mDownloadTaskId = cursor.getString(0);
					mNextbasicDownload.mUrl = cursor.getString(1);
					mNextbasicDownload.mPriority = cursor.getInt(2);
					mNextbasicDownload.mServerVc = cursor.getInt(3);
					mNextbasicDownload
							.updataTaskState(BasicDownload.DOWNLOAD_STATTE_DOWNLOADING);
					Log.d("download", mNextbasicDownload.mDownloadTaskId
							+ " state= " + cursor.getString(4));

				} catch (Exception e) {
					e.printStackTrace();

					Log.d("download", "反射调用异常==" + e.getMessage());

				}

			} else {
				mNextbasicDownload = null;
			}

			cursor.close();

			return mNextbasicDownload;
		}
	}

}
